<?php


namespace App\Repositories;

//红包
use App\Exceptions\BasicException;

class RedPacketRepository
{
    /**
     * 查询私聊红包记录
     * @param array $params 查询参数
     * @param int $isPaging 是否分页，1：是；0：否
     * @return array
     */
    public function getPrivateRedPacketRecords($params, $isPaging = 1)
    {
        $createTimeStart = $params['create_time_start'] ?? '';
        $createTimeEnd = $params['create_time_end'] ?? '';
        $userCode = $params['user_code'] ?? 0;
        $nickname = $params['nickname'] ?? '';
        $status = $params['status'] ?? -1;
        $pageSize = $params['page_size'] ?? 10;

        $where = [];
        $whereFunc = [];
        if ($createTimeStart) {
            $where[] = ['created_at', '>=', $createTimeStart . ' 00:00:00'];
        }
        if ($createTimeEnd) {
            $where[] = ['created_at', '<=', $createTimeEnd . ' 23:59:59'];
        }
        if ($userCode) {
            $userId = app('model_user_data')->where('user_code', $userCode)->value('user_id');
            $where[] = ['send_user_id', '=', $userId];
        }
        if ($nickname) {
            $userIds = app('model_user_data')->where('nickname', 'like', "%$nickname%")->pluck('user_id')->toArray();
            $whereFunc = function ($query) use ($userIds) {
                $query->whereIn('send_user_id', $userIds);
            };
        }
        if ($status != -1) {
            $where[] = ['status', '=', $status];
        }

        $list = app('model_private_chat_rp_record')
            ->select('id', 'send_user_id', 'receive_user_id', 'money', 'cost', 'status', 'created_at')
            ->where($where)
            ->where($whereFunc);
        $allPrice = 0;
        if ($isPaging == 1) {
            $allPrice = $list->sum('money');
            $list = $list->paginate($pageSize)->toArray();
            $data = $list['data'];
        } else {
            $data = $list = $list->get()->toArray();
            if (count($data) > 10000) {
                throw new BasicException(0, '一次最多导出1万条数据，请选择筛选条件');
            }
        }

        //批量查询用户ID和昵称
        $sendUserIds = array_column($data, 'send_user_id');
        $receiveUserIds = array_column($data, 'receive_user_id');
        $userIds = array_merge($sendUserIds, $receiveUserIds);
        $userDatas = app('model_user_data')->batchQueryByUserIdMap($userIds);

        $statusDescMap = [0 => '未领取', 1 => '已领取', 2 => '已失效'];
        foreach ($data as &$v) {
            $v['send_user_code'] = $userDatas[$v['send_user_id']]['user_code'] ?? '';
            $v['send_nickname'] = $userDatas[$v['send_user_id']]['nickname'] ?? '';
            $v['receive_user_code'] = $userDatas[$v['receive_user_id']]['user_code'] ?? '';
            $v['receive_nickname'] = $userDatas[$v['receive_user_id']]['nickname'] ?? '';
            $v['status_desc'] = $statusDescMap[$v['status']];
        }
        if ($isPaging == 1) {
            $list['data'] = $data;
        } else {
            $list = $data;
        }
        $list['all_price'] = $allPrice;
        return $list;
    }
}